//
//  MovieAddView.m
//  基本框架
//
//  Created by runone02 on 2018/8/21.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "MovieAddView.h"

@implementation MovieAddView


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
        self.count = -1;
    }
    return self;
}

- (void)setupUI {
    self.v_main = [[UIView alloc]init];
    self.v_main.backgroundColor = RGBA(255, 246, 241, 1);
    [self addSubview:self.v_main];
    [self.v_main mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(self);
    }];
    
    self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.backButton setTitle:@"X" forState:UIControlStateNormal];
    [self.backButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [self.backButton addTarget:self action:@selector(backButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.v_main addSubview:self.backButton];
    
    [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(30);
        make.left.mas_equalTo(self.v_main).with.offset(5);
        make.width.height.mas_equalTo(30);
    }];
    self.v_count = [[UIView alloc]init];

    [self.v_main addSubview:self.v_count];
    [self.v_count mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.v_main).with.offset(50);
        make.centerX.mas_equalTo(self.v_main);
        make.width.height.mas_equalTo(120);
    }];
    self.lbl_count = [[UILabel alloc]init];
    self.lbl_count.textColor = [UIColor redColor];
    self.lbl_count.font = TITLE_FONT(60);
    
    [self.v_count addSubview:self.lbl_count];
    [self.lbl_count mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.centerX.mas_equalTo(self.v_count);
        
    }];
    
    self.la = [[UILabel alloc]init];
    
    self.la.font = TITLE_FONT(25);
    self.la.textColor = [UIColor lightGrayColor];
    [self.v_count addSubview:self.la];
    [self.la mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.lbl_count);
        make.left.mas_equalTo(self.lbl_count.mas_right).with.offset(10);
    }];
    
    self.lbl_counttext = [[UILabel alloc]init];
    self.lbl_counttext.text = @"请滑动星星评分";
    self.lbl_counttext.font = TITLE_FONT(30);
    self.lbl_counttext.textAlignment = NSTextAlignmentCenter;
    [self.v_main addSubview:self.lbl_counttext];
    [self.lbl_counttext mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.v_count.mas_bottom).with.offset(10);
        make.left.mas_equalTo(self.v_main);
        make.right.mas_equalTo(self.v_main);
        make.height.mas_equalTo(20);
    }];
    
    self.v_star = [[UIView alloc]init];
    [self.v_main addSubview:self.v_star];
    [self.v_star mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.lbl_counttext.mas_bottom).with.offset(20);
        make.left.mas_equalTo(self.v_main).with.offset(30);
        make.right.mas_equalTo(self.v_main).with.offset(-30);
        make.height.mas_equalTo(50);
    }];
    
    self.img_star3 = [[UIImageView alloc]init];
    self.img_star3.image = IMAGE(@"StarUnSelect");
    [self.v_star addSubview:self.img_star3];
    [self.img_star3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.v_star);
        make.centerY.mas_equalTo(self.v_star);
    }];
    
    self.img_star2 = [[UIImageView alloc]init];
    self.img_star2.image = IMAGE(@"StarUnSelect");
    [self.v_star addSubview:self.img_star2];
    [self.img_star2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.v_star);
        make.right.mas_equalTo(self.img_star3.mas_left).with.offset(-10);
    }];
    
    self.img_star1 = [[UIImageView alloc]init];
    self.img_star1.image = IMAGE(@"StarUnSelect");
    [self.v_star addSubview:self.img_star1];
    [self.img_star1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.v_star);
        make.right.mas_equalTo(self.img_star2.mas_left).with.offset(-10);
    }];
    
    self.img_star4 = [[UIImageView alloc]init];
    self.img_star4.image = IMAGE(@"StarUnSelect");
    [self.v_star addSubview:self.img_star4];
    [self.img_star4 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.v_star);
        make.left.mas_equalTo(self.img_star3.mas_right).with.offset(10);
    }];
    
    self.img_star5 = [[UIImageView alloc]init];
    self.img_star5.image = IMAGE(@"StarUnSelect");
    [self.v_star addSubview:self.img_star5];
    [self.img_star5 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.v_star);
        make.left.mas_equalTo(self.img_star4.mas_right).with.offset(10);
    }];
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)backButtonClick:(UIButton *)btn{
    [self.viewController.navigationController popViewControllerAnimated:YES];
}

-(void)hidKeyboard:(id)sender{
    [self endEditing:YES];
}

-(void)cleamCount{
    self.v_count.hidden = YES;
    self.lbl_counttext.text = @"请滑动星星评分";
    self.count = -1;
    
    [self.img_star1 setImage:[UIImage imageNamed:@"StarUnSelect"]];
    [self.img_star2 setImage:[UIImage imageNamed:@"StarUnSelect"]];
    [self.img_star3 setImage:[UIImage imageNamed:@"StarUnSelect"]];
    [self.img_star4 setImage:[UIImage imageNamed:@"StarUnSelect"]];
    [self.img_star5 setImage:[UIImage imageNamed:@"StarUnSelect"]];
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.v_star];
    if((point.x>0 && point.x<self.v_star.frame.size.width)&&(point.y>0 && point.y<self.v_star.frame.size.height)){
        self.canAddStar = YES;
        [self changeStarForegroundViewWithPoint:point];
        
    }else{
        self.canAddStar = NO;
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    
    if(self.canAddStar){
        UITouch *touch = [touches anyObject];
        CGPoint point = [touch locationInView:self.v_star];
        [self changeStarForegroundViewWithPoint:point];
        
    }
    
    
    return;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if(self.canAddStar){
        UITouch *touch = [touches anyObject];
        CGPoint point = [touch locationInView:self.v_star];
        [self changeStarForegroundViewWithPoint:point];
    }
    self.canAddStar = NO;

    return;
}


- (void)changeStarForegroundViewWithPoint:(CGPoint)point{
    if(self.v_count.hidden){
        self.v_count.hidden = NO;
    }
    self.v_count.layer.borderWidth = 1.0f;
    self.v_count.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.v_count.layer.cornerRadius = 60;
    self.v_count.clipsToBounds = YES;

    NSInteger count = 0;
    count = count + [self changeImg:point.x image:self.img_star1];
    count = count + [self changeImg:point.x image:self.img_star2];
    count = count + [self changeImg:point.x image:self.img_star3];
    count = count + [self changeImg:point.x image:self.img_star4];
    count = count + [self changeImg:point.x image:self.img_star5];
    if(count == 0){
        count = 1;
        [self.img_star1 setImage:[UIImage imageNamed:@"StarSelectHeaf"]];
    }
    self.count = count;
    [self checkCount:count];
    if(count == 10){
        self.lbl_count.text = @"10";
        self.la.text = @"";
    }else{
        self.lbl_count.text = [NSString stringWithFormat:@"%ld",count];
        self.la.text = @"分";
    }
}

- (void)checkCount:(NSInteger)count{
    switch (count) {
        case 1:
        case 2:
            self.lbl_counttext.text = @"很差，不推荐";
            break;
        case 3:
        case 4:
            self.lbl_counttext.text = @"凑合，可考虑";
            break;
        case 5:
        case 6:
            self.lbl_counttext.text = @"一般，还值得";
            break;
        case 7:
        case 8:
            self.lbl_counttext.text = @"不错，要推荐";
            break;
        case 9:
        case 10:
            self.lbl_counttext.text = @"完美，不错过";
            break;
        default:
            break;
    }
}

-(NSInteger)changeImg:(float)x image:(UIImageView*)img{
    if(x> img.frame.origin.x + img.frame.size.width/2){
        [img setImage:[UIImage imageNamed:@"StarSelected"]];
        return 2;
    }else if(x> img.frame.origin.x){
        [img setImage:[UIImage imageNamed:@"StarSelectHeaf"]];
        return 1;
        //        [self setImageAnimation:img];
    }else{
        [img setImage:[UIImage imageNamed:@"StarUnSelect"]];
        return 0;
    }
}

- (void)setImageAnimation:(UIView *)v{
    CGRect rec = v.frame;
    [UIView animateWithDuration:0.1 animations:^{
        v.frame = CGRectMake(v.frame.origin.x, v.frame.origin.y -3, v.frame.size.width, v.frame.size.height);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.1 animations:^{
            v.frame = rec;
        } completion:^(BOOL finished) {
            v.frame = rec;
        }];
    }];
}


@end
